package iq.number;

import java.util.ArrayList;
import java.util.List;

public class Binary {
	static final int[] powResults = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483647};
	/**
	 * �������ַ�ת������
	 * @param binaryStr
	 * @return
	 */
	public static int parseBinary(String binaryStr) {
		int result = 0;
		int length = binaryStr.length();
		for (int i=0; i<length; i++) {
			int num = Integer.parseInt(binaryStr.charAt(i) + "");
			if (num > 1) {
				throw new NumberFormatException(binaryStr + " is not binary charactor.");
			}
			result += num * Math.pow(2, length-i-1);
		}
		return result;
	}
	
	/**
	 * ��������ʾ
	 * @param i
	 * @return
	 */
	public static String toBinaryString(int i) {
		return Integer.toBinaryString(i);
	}
	
	/**
	 * �ָ�����ӵĶ������ַ�
	 * @param binaryString
	 * @return
	 */
	public static List<Integer> split(int num) {
		List<Integer> list = new ArrayList<Integer>();
		int index = 0;
		while (true) {
			if (num >= powResults[index+1]) {
				index++;
				if (index == powResults.length-1) {
					if (num == powResults[index]) {
						break;
					} else {
						throw new ArrayIndexOutOfBoundsException();
					}
				}
			} else {
				break;
			}
		}
		for (int i=index; i>=0; i--) {
			int a = powResults[i] * (num/powResults[i]);
			if (a > 0) {
				list.add(a);
			}
			num = num % powResults[i];
		}
		return list;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List<Integer> list = split(2147483646);
		for (int a : list) {
			System.out.println(a);
		}
		Integer integer = null;
		//int a = integer; //error
		
		Integer scope = 3208;
		String[] ids = new String[4];
		if (scope != null && scope > 0) {
			ids = new String[4];
			ids[0] = 1000*(scope/1000) + "";
			ids[1] = 100*((scope - 1000*(scope/1000))/100) + "";
			ids[2] = 10*((scope - 100*(scope/100))/10) + "";
			ids[3] = (scope - 10*(scope/10)) + "";
		}
	}

}
